//
//  ShareWindow.m
//  SunXi
//
//  Created by dllo on 15/12/6.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "ShareWindow.h"
#import "AppDelegate.h"
#import <time.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/WeiBoAPI.h>
#import <MediaPlayer/MediaPlayer.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <CommonCrypto/CommonDigest.h>
#import <TencentOpenAPI/TencentApiInterface.h>
#import <TencentOpenAPI/TencentMessageObject.h>
#import <TencentOpenAPI/QQApiInterface.h>
@implementation ShareWindow

+ (ShareWindow *)sharedInstance
{
    static id sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    });
    return sharedInstance;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0.580 green:0.583 blue:0.583 alpha:0.500];
        UIButton *chabutton = [UIButton buttonWithType:UIButtonTypeSystem];
        chabutton.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width / 2 - 25, [[UIScreen mainScreen] bounds].size.height - 80, 50, 50);
        [chabutton setImage:[UIImage imageNamed:@"dcha"] forState:0];
        [self addSubview:chabutton];
        [chabutton setTintColor:BUTTONCOLOR];
        [chabutton addTarget:self action:@selector(hidd) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        UIButton *friendbutton = [UIButton buttonWithType:UIButtonTypeSystem];
        friendbutton.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width / 2 - 75, [[UIScreen mainScreen] bounds].size.height / 4 - 20, 150, 40);
        friendbutton.backgroundColor = BUTTONCOLOR;
        [self addSubview:friendbutton];
        [friendbutton setTitle:@"我的好友" forState:0];
        [friendbutton setTintColor:[UIColor whiteColor]];
        [friendbutton addTarget:self action:@selector(myfriend) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *weChatButton = [UIButton buttonWithType:UIButtonTypeSystem];
        weChatButton.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width / 2 - 75, [[UIScreen mainScreen] bounds].size.height / 4 + 40, 150, 40);
        weChatButton.backgroundColor = BUTTONCOLOR;
        [self addSubview:weChatButton];
        [weChatButton setTitle:@"微信" forState:0];
        [weChatButton setTintColor:[UIColor whiteColor]];
        [weChatButton addTarget:self action:@selector(ShareWeChat:) forControlEvents:UIControlEventTouchUpInside];
        
        
        UIButton *qqButton = [UIButton buttonWithType:UIButtonTypeSystem];
        qqButton.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width / 2 - 75, [[UIScreen mainScreen] bounds].size.height / 4 + 100, 150, 40);
        qqButton.backgroundColor = BUTTONCOLOR;
        [self addSubview:qqButton];
        [qqButton setTitle:@"QQ" forState:0];
        [qqButton setTintColor:[UIColor whiteColor]];
        [qqButton addTarget:self action:@selector(ShareQQ) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *weiBoButton = [UIButton buttonWithType:UIButtonTypeSystem];
        weiBoButton.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width / 2 - 75, [[UIScreen mainScreen] bounds].size.height / 4 + 160, 150, 40);
        weiBoButton.backgroundColor = BUTTONCOLOR;
        [self addSubview:weiBoButton];
        [weiBoButton setTitle:@"新浪微博" forState:0];
        [weiBoButton setTintColor:[UIColor whiteColor]];
        [weiBoButton addTarget:self action:@selector(ShareWeiBo) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        
        
        
        
        
    }
    return self;
}
- (void)show {
    [self makeKeyWindow];
    self.hidden = NO;
}
- (void)hidd{
    [self resignKeyWindow];
    self.hidden = YES;
    self.rootViewController = nil;
}
- (void)myfriend{
    UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:[[shareFriendViewController alloc]init]];
    self.rootViewController = naVC;
}
- (void)ShareWeChat:(UIButton *)button{
    static BOOL one = YES;
    if (one) {
        one = NO;
        button.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width / 2 - 40, [[UIScreen mainScreen] bounds].size.height / 4 + 40, 80, 40);
        [button setTitle:@"朋友圈" forState:0];
        UIButton *weChatButton = [UIButton buttonWithType:UIButtonTypeSystem];
        weChatButton.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width / 2 - 40, [[UIScreen mainScreen] bounds].size.height / 4 + 40, 80, 40);
        weChatButton.backgroundColor = BUTTONCOLOR;
        [self addSubview:weChatButton];
        [weChatButton setTitle:@"好友" forState:0];
        [weChatButton setTintColor:[UIColor whiteColor]];
        [weChatButton addTarget:self action:@selector(ShareWeChatpeople) forControlEvents:UIControlEventTouchUpInside];
        
        [UIView animateWithDuration:1 animations:^{
            //动画部分都写在block里
            button.center = CGPointMake(button.center.x - 45, button.center.y);
            weChatButton.center = CGPointMake(weChatButton.center.x + 45, weChatButton.center.y);
        }];
        
        
        
        
        
        
        
    }else{
        [self ShareWeChatquan];
    }
    
    
    
}
- (void)ShareWeChatquan{
   

    NSString *shaStr;
    switch ([shareModel sharedInstance].type) {
        case Look: {
            shaStr = [NSString stringWithFormat:@"瞬息文章:%@链接:%@",[shareModel sharedInstance].title,[shareModel sharedInstance].code];
            break;
        }
        case Music: {
            NSDictionary *dic = [DyAFnetWorking convertStrToBodyDic:[shareModel sharedInstance].code];
            NSLog(@"%@",dic[@"url"]);
            
            shaStr = [NSString stringWithFormat:@"瞬息电台:%@链接:%@",[shareModel sharedInstance].title,dic[@"url"]];
            
            break;
        }
        case Movie: {
            shaStr = [NSString stringWithFormat:@"瞬息视频:%@链接:%@",[shareModel sharedInstance].title,[shareModel sharedInstance].code];
            
            break;
        }
        default: {
            break;
        }
    }
    
    if ([WXApi isWXAppInstalled] && [WXApi isWXAppSupportApi]) {
        SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
        req.bText = YES;
        req.text = shaStr;
        req.scene = WXSceneTimeline;
        
        [WXApi sendReq:req];
    } else{
        [ShowMytext showHUDTextWithTitle:@"你还没有微信呢" View:self];
           }
    

}
- (void)ShareWeChatpeople{
    NSString *shaStr;
    switch ([shareModel sharedInstance].type) {
        case Look: {
            shaStr = [NSString stringWithFormat:@"瞬息文章:%@链接:%@",[shareModel sharedInstance].title,[shareModel sharedInstance].code];
            break;
        }
        case Music: {
            NSDictionary *dic = [DyAFnetWorking convertStrToBodyDic:[shareModel sharedInstance].code];
            NSLog(@"%@",dic[@"url"]);
            
            shaStr = [NSString stringWithFormat:@"瞬息电台:%@链接:%@",[shareModel sharedInstance].title,dic[@"url"]];
            
            break;
        }
        case Movie: {
            shaStr = [NSString stringWithFormat:@"瞬息视频:%@链接:%@",[shareModel sharedInstance].title,[shareModel sharedInstance].code];
            
            break;
        }
        default: {
            break;
        }
    }
    
    if ([WXApi isWXAppInstalled] && [WXApi isWXAppSupportApi]) {
        SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
        req.bText = YES;
        req.text = shaStr;
        req.scene = WXSceneSession;
        
        [WXApi sendReq:req];
    } else{
        [ShowMytext showHUDTextWithTitle:@"你还没有微信呢" View:self];

    }
}
- (void)ShareQQ {
    
    NSString *shaStr;
    switch ([shareModel sharedInstance].type) {
        case Look: {
            shaStr = [NSString stringWithFormat:@"瞬息文章:%@链接:%@",[shareModel sharedInstance].title,[shareModel sharedInstance].code];
            break;
        }
        case Music: {
            NSDictionary *dic = [DyAFnetWorking convertStrToBodyDic:[shareModel sharedInstance].code];
            NSLog(@"%@",dic[@"url"]);
            
            shaStr = [NSString stringWithFormat:@"瞬息电台:%@链接:%@",[shareModel sharedInstance].title,dic[@"url"]];
            
            break;
        }
        case Movie: {
            shaStr = [NSString stringWithFormat:@"瞬息视频:%@链接:%@",[shareModel sharedInstance].title,[shareModel sharedInstance].code];
            
            break;
        }
        default: {
            break;
        }
    }

    QQApiTextObject *txtObj = [QQApiTextObject objectWithText:shaStr];
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:txtObj];
    //将内容分享到qq
    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    [self hidd];
}

- (void)ShareWeiBo {
    
    UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:[[WBViewController alloc]init]];
    self.rootViewController = naVC;
}


@end
